package tech.chauncy.springbootwebsample.controller;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tech.chauncy.springbootwebsample.entity.ConditionEntity;
import tech.chauncy.springbootwebsample.util.YamlUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunjia on 2018/3/7.
 */

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private YamlUtil yamlUtil;
    private static final Logger logger = Logger.getLogger(TestController.class);


    @RequestMapping(value = "/csf_tjpd", method = RequestMethod.POST)
    public ResponseEntity<Object> tjpd(HttpServletRequest request,
                                         @RequestBody(required = true) String jsonStr) throws Exception {


        logger.info(jsonStr);
        JSONObject json = parserAPI("tjpd",jsonStr);
        //Thread.sleep(2000);
        logger.info(json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }


    @RequestMapping(value = "/csf_dxxf", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    public ResponseEntity<Object> dxxf(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {


        JSONObject json = new JSONObject();
        logger.info(jsonStr);
        JSONObject subjson = new JSONObject();
        subjson.put("结果", "下发成功");
        json.put("code", "0");
        json.put("message", "成功");
        json.put("result", subjson);
        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);

    }


    @RequestMapping(value = "/csf_mmsfzq", method = RequestMethod.POST)
    public ResponseEntity<Object> mmsfzq(HttpServletRequest request,
                                             @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info(jsonStr);
        JSONObject json = parserAPI("mmsfzq",jsonStr);
        logger.info(json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_syllcx", method = RequestMethod.POST)
    public ResponseEntity<Object> syllcx(HttpServletRequest request,
                                         @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info(jsonStr);
        JSONObject json = parserAPI("syllcx",jsonStr);
        logger.info(json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }


    @RequestMapping(value = "/csf_mmxg", method = RequestMethod.POST)
    public ResponseEntity<Object> mmxg(HttpServletRequest request,
                                         @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info(jsonStr);
        JSONObject json = parserAPI("mmxg",jsonStr);
        logger.info(json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_sfzhq", method = RequestMethod.POST)
    public ResponseEntity<Object> sfzhq(HttpServletRequest request,
                                        @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("sfzhq",jsonStr);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_swgg", method = RequestMethod.POST,produces="text/html;charset=UTF-8")

    public ResponseEntity<Object> swgg(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("swgg",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_swggkt", method = RequestMethod.POST)
    public ResponseEntity<Object> swggkt(HttpServletRequest request,
                                         @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("swggkt",jsonStr);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_syll", method = RequestMethod.POST)
    public ResponseEntity<Object> syll(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("syll",jsonStr);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_swggqx1", method = RequestMethod.POST)
    public ResponseEntity<Object> swggqx1(HttpServletRequest request,
                                          @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("swggqx1",jsonStr);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_swggqx2", method = RequestMethod.POST)
    public ResponseEntity<Object> swggqx2(HttpServletRequest request,
                                          @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("swggqx2",jsonStr);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_yhtccf", method = RequestMethod.POST)
    public ResponseEntity<Object> yhtccf(HttpServletRequest request,
                                         @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("yhtccf",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_tcjr", method = RequestMethod.POST)
    public ResponseEntity<Object> tcjr(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("tcjr",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_gprssfd", method = RequestMethod.POST)
    public ResponseEntity<Object> gprssfd(HttpServletRequest request,
                                          @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("gprssfd",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_swtc", method = RequestMethod.POST)
    public ResponseEntity<Object> swtc(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("swtc",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_llbbl", method = RequestMethod.POST)
    public ResponseEntity<Object> llbbl(HttpServletRequest request,
                                        @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("llbbl",jsonStr);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_swllqx", method = RequestMethod.POST)
    public ResponseEntity<Object> swllqx(HttpServletRequest request,
                                         @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("swllqx",jsonStr);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_lljyb", method = RequestMethod.POST)
    public ResponseEntity<Object> lljyb(HttpServletRequest request,
                                        @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("lljyb",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_qnllcsbcx10", method = RequestMethod.POST)
    public ResponseEntity<Object> qnllcsbcx10(HttpServletRequest request,
                                              @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("qnllcsbcx10",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_qnllcsbcx", method = RequestMethod.POST)
    public ResponseEntity<Object> qnllcsbcx(HttpServletRequest request,
                                            @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("qnllcsbcx",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_qnllcsbqx", method = RequestMethod.POST)
    public ResponseEntity<Object> qnllcsbqx(HttpServletRequest request,
                                            @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("qnllcsbqx",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_qnllcsb", method = RequestMethod.POST)
    public ResponseEntity<Object> qnllcsb(HttpServletRequest request,
                                          @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("qnllcsb",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_tjbl", method = RequestMethod.POST)
    public ResponseEntity<Object> tjbl(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("tjbl",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_kjbl", method = RequestMethod.POST)
    public ResponseEntity<Object> kjbl(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("kjbl",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_zzyw", method = RequestMethod.POST)
    public ResponseEntity<Object> zzyw(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("zzyw",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_ktywcx", method = RequestMethod.POST)
    public ResponseEntity<Object> ktywcx(HttpServletRequest request,
                                         @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("ktywcx",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_hfcx", method = RequestMethod.POST)
    public ResponseEntity<Object> hfcx(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("hfcx",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_smrz", method = RequestMethod.POST)
    public ResponseEntity<Object> smrz(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("smrz",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_swllcx", method = RequestMethod.POST)
    public ResponseEntity<Object> swllcx(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("swllcx",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_gprsqx", method = RequestMethod.POST)
     public ResponseEntity<Object> gprsqx(HttpServletRequest request,
                                          @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("gprsqx",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_sfxgllb", method = RequestMethod.POST)
    public ResponseEntity<Object> sfxgllb(HttpServletRequest request,
                                         @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info (jsonStr);
        JSONObject json = parserAPI("sfxgllb",jsonStr);
        //Thread.sleep(2000);
        logger.info (json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_czjl", method = RequestMethod.POST)
    public ResponseEntity<Object> czjl(HttpServletRequest request,
                                      @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info(jsonStr);
        JSONObject json = parserAPI("czjl",jsonStr);
        logger.info(json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_fhjl", method = RequestMethod.POST)
    public ResponseEntity<Object> fhjl(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        logger.info(jsonStr);
        JSONObject json = parserAPI("fhjl",jsonStr);
        logger.info(json);

        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_gsdcx", method = RequestMethod.POST)
    public ResponseEntity<Object> gsdcx(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {
        logger.info(jsonStr);
        JSONObject json = parserAPI("gsdcx",jsonStr);
        logger.info(json);
        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_sfblkd", method = RequestMethod.POST)
    public ResponseEntity<Object> sfblkd(HttpServletRequest request,
                                        @RequestBody(required = true) String jsonStr) throws Exception {
        logger.info(jsonStr);
        JSONObject json = parserAPI("sfblkd",jsonStr);
        logger.info(json);
        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_kdmmcz", method = RequestMethod.POST)
    public ResponseEntity<Object> kdmmcz(HttpServletRequest request,
                                         @RequestBody(required = true) String jsonStr) throws Exception {
        logger.info(jsonStr);
        JSONObject json = parserAPI("kdmmcz",jsonStr);
        logger.info(json);
        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

//    @RequestMapping(value = "/csf_dxxf", method = RequestMethod.POST)
//    public ResponseEntity<Object> dxxf(HttpServletRequest request,
//                                       @RequestBody(required = true) String jsonStr) throws Exception {
//
//
//        JSONObject json = new JSONObject();
//        JSONObject subjson = new JSONObject();
//        subjson.put("城市", "北京");
//        json.put("code", "0");
//        json.put("message", "成功");
//        json.put("result", subjson);
//        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
//    }



    @RequestMapping(value = "/csf_cxwz", method = RequestMethod.POST)
    public ResponseEntity<Object> cxwz(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {


        JSONObject json = new JSONObject();
        JSONObject subjson = new JSONObject();
        subjson.put("城市", "北京");
        json.put("code", "0");
        json.put("message", "成功");
        json.put("result", subjson);
        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_ctq", method = RequestMethod.POST)
    public ResponseEntity<Object> ctq(HttpServletRequest request,
                                       @RequestBody(required = true) String jsonStr) throws Exception {

        JSONObject json = new JSONObject();
        JSONObject subjson = new JSONObject();
        subjson.put("天气情况", "良");
        json.put("code", "0");
        json.put("message", "成功");
        json.put("result", subjson);
        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }

    @RequestMapping(value = "/csf_welcome", method = RequestMethod.POST)
    public ResponseEntity<Object> welcome(HttpServletRequest request,
                                      @RequestBody(required = true) String jsonStr) throws Exception {

        JSONObject json = new JSONObject();
        JSONObject subjson = new JSONObject();
        subjson.put("欢迎语", "欢迎语");
        subjson.put("突发场景语", "突发场景语");
        json.put("code", "0");
        json.put("message", "成功");
        json.put("result", subjson);
        return new ResponseEntity<Object>(json.toString(), HttpStatus.OK);
    }


    private JSONObject parserAPI(String APIName, String jsonStr){

        JSONObject injson = new JSONObject(jsonStr);
        String userid = injson.getJSONObject("params").getString("tel");
        JSONObject json = new JSONObject();
        JSONObject subjson = new JSONObject();
        if (yamlUtil.getById(userid).getOutputEntityByName(APIName)==null){
            json.put("code", "-1");
            json.put("message", "失败原因：DM调用此接口但yaml文件没有配置。接口名为"+APIName+"。");
            json.put("result", subjson);
        }else {
            List<String> conditionStrList = yamlUtil.getById(userid).getConditionStrListByName(APIName);
            for (String condition : conditionStrList) {
                //认为目前的condition有两种情况：1 == 2 !=
                String result = yamlUtil.getById(userid).getConditionEntityByNameAndCondition(APIName, condition).getResult();
                if (condition.contains("==")) {
                    subjson = parserCondition(condition, result, "==", true, injson);
                    if (subjson.toString().trim().length() > 2) {
                        break;
                    }
                } else if (condition.contains("!=")) {
                    subjson = parserCondition(condition, result, "!=", false, injson);
                } else if (condition.contains("true")) {
                    subjson = parserResult(result);
                }
            }
            json.put("code", "0");
            json.put("message", "成功");
            json.put("result", subjson);
        }
        return json;

    }


    private JSONObject parserCondition(String condition, String result, String sign, boolean flag, JSONObject injson) {
        JSONObject subjson = new JSONObject();
        String[] conditionParse = condition.split(sign);
        String postParam = conditionParse[0].trim();
        String goldValue = conditionParse[1].trim();
        if (injson.getJSONObject("params").getString(postParam).equals(goldValue) == flag) {
            subjson = parserResult(result);
        }
        return subjson;
    }

    private JSONObject parserResult(String result){

        JSONObject subjson = new JSONObject();
        String[] resultls = result.split(",");
        for (String res : resultls) {
            String[] resElement = res.split("=");
            subjson.put(resElement[0], resElement[1]);
        }
        return subjson;
    }

}
